<% content_for :head do -%>
<script type="text/javascript">
  var popHealth = popHealth || {};

  popHealth.promoteUser = function() {
    var username = $(this).data("username");
    var role = $(this).data("role");
    var parentTd = $(this).parent();
    $.post('<%= admin_promote_path %>', {'username': username, 'role': role}, function(data) {
      parentTd.html(data);
      parentTd.find('.demote').click(popHealth.demoteUser);
    });
    return false;
  };

  popHealth.demoteUser = function() {
    var username = $(this).data("username");
    var role = $(this).data("role");
    var parentTd = $(this).parent();

    if (username == '<%= current_user.username %>') {
      var response = confirm('You are about to remove admin rights from the currently logged in user' 
      +', this will cause you to immediately lose access to all admin functionality including this page.  ' 
      +'You will need to use another admin account to restore admin rights.  Are you sure you want to continue?');
      if (!response) return;
    }

    $.post('<%= admin_demote_path %>', {'username': username, 'role': role}, function(data) {
      parentTd.html(data);
      parentTd.find('.promote').click(popHealth.promoteUser);
    });
    return false;
  };

  popHealth.approveUser = function() {
    var username = $(this).data("username");
    var parentTd = $(this).parent();
    $.post('<%= admin_approve_path %>', {'username': username}, function(data) {
      parentTd.html(data);
    });
  };

  popHealth.disableUser = function() {
    var username = $(this).data("username");
    var parentTd = $(this).parent();

    if (username == '<%= current_user.username %>') {
      var response = confirm('You are about to disable the currently logged in user' 
      +', this will cause you to immediately lose access and be logged out.  Are you sure you want to continue?');
      if (!response) return;
    }

    popHealth.disableEnableUser(1, username, parentTd)
  };
  popHealth.enableUser = function() {
    var username = $(this).data("username");
    var parentTd = $(this).parent();
    popHealth.disableEnableUser(0, username, parentTd)
  };
  popHealth.disableEnableUser = function(disabled, username, parentTd) {
    $.post('<%= admin_disable_path %>', {'username': username, 'disabled': disabled}, function(data) {
      parentTd.html(data);
      if (disabled == 1) parentTd.find('.disable').click(popHealth.enableUser);
      else parentTd.find('.enable').click(popHealth.disableUser);
    });
  };
  popHealth.changeNpi = function() {
    var username = $(this).data("username");
    var npi = $(this).val();
    $.post('<%= admin_update_npi_path %>', {'username': username, 'npi': npi}, function(data) {
    });
    
  };

  $(document).ready(function() {
    $('.promote').click(popHealth.promoteUser);
    $('.demote').click(popHealth.demoteUser);
    $('.approve').click(popHealth.approveUser);
    $('.enable').click(popHealth.disableUser);
    $('.disable').click(popHealth.enableUser);
    $('.npi_select').change(popHealth.changeNpi);
  });
</script>
<% end -%>

<div id="pageContent">
<h1>Users</h1>
<table id="patientTable">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Username</th>
    <th>provider</th>
    <th>Administrator</th>
    <th>Staff Role</th>
    <th>Approved</th>
    <th>Status</th>
  </tr>
  <% @users.each do |user| -%>
  <tr>
    <td><%= user.first_name %></td>
    <td><%= user.last_name %></td>
    <td><%= user.username %></td>
    <td>
      <select class="npi_select" data-username="<%= user.username %>" name="user[npi]">
        <option value="-1">&nbsp;</option>
        <% Provider.all.alphabetical.each do |provider| %>
          <option value="<%= provider.npi %>" <%= (user.npi == provider.npi) ? "selected=\"selected\"" : ""%>><%= provider.full_name%></option>
        <% end%>
      </select>
    </td>
    <td>
      <% if user.admin? -%>
      Yes - <a href="#" class="demote" data-role="admin" data-username="<%= user.username %>">revoke</a>
      <% else -%>
      No - <a href="#" class="promote" data-role="admin" data-username="<%= user.username %>">grant</a>
      <% end -%>
    </td>
    <td>
      <% if user.staff_role? -%>
      Yes - <a href="#" class="demote" data-role="staff_role" data-username="<%= user.username %>">revoke</a>
      <% else -%>
      No - <a href="#" class="promote" data-role="staff_role" data-username="<%= user.username %>">grant</a>
      <% end -%>
    </td>
    <td>
      <% if user.approved? -%>
      true
      <% else -%>
      <a href="#" class="approve" data-username="<%= user.username %>">approve</a>
      <% end -%>
    </td>
    <td>
      <a href="#" class="<%= (user.disabled?) ? 'disable' : 'enable'%>" data-username="<%= user.username %>"><%= (user.disabled?) ? 'disabled' : 'enabled'%></a>
    </td>
  </tr>
  <% end -%>
</table>
</div> <!-- pageContent -->
